<template>
  <div class="container">
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <div class="users_main_head">
        <el-form-item label="关键词">
          <el-input
            class="users_main_head_search"
            v-model="form.keyword"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm">清空</el-button>
          <el-button type="primary" @click="onSubmit">搜索</el-button>
          <el-button
            type="success"
            class="newElement"
            @click="openDrawer('add')"
            >新增技巧</el-button
          >
        </el-form-item>
      </div>
      <div class="uesrs_main_statistics">共{{ total }}条记录</div>
      <el-table ref="table" :data="randerList" style="width: 100%">
        <el-table-column prop="id" label="序号" width="90"> </el-table-column>
        <el-table-column prop="title" label="文章标题" width="200">
          <template slot-scope={row}>
            <span style="margin-right: 10px">{{ row.title }}</span>
            <i @click="val(row.videoURL)" v-if="row.videoURL" class="el-icon-film" ></i>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅览数" width="220">
        </el-table-column>
        <el-table-column prop="username" label="录入人" width="200">
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="210">
        </el-table-column>
        <el-table-column prop="state" label="状态" width="150">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <a @click="preview(row.id)">预览</a>
            <a v-if="row.state !== '已启用'" @click="isState(row.id, row.state)"
              >启用</a
            >
            <a v-else @click="isState(row.id, row.state)">禁用</a>
            <el-button type="text" @click="openDrawer('edit', row.id)" :disabled="row.state === '已启用'">修改</el-button>
            <el-button type="text"  @click="delDrawer(row.id)" :disabled="row.state === '已启用'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="query.pagesize"
        layout=" prev, pager, next,sizes, jumper"
        :total="total"
      >
      </el-pagination>
    </el-form>
      <el-dialog width='30%' :before-close="handleVal" :visible="isShow">
  <video :src="valUrl"></video>
  <div>{{valUrl}}</div>
</el-dialog>
    <ArticlesAdd
      @newDataes="newDataes"
      :dialogFormVisible.sync="dialogFormVisible"
      :formBase="formBase"
    ></ArticlesAdd>
    <ArticlesPreview
      :item="previewData"
      :isDialogFormVisible.sync="isDialogFormVisible"
    ></ArticlesPreview>
  </div>
</template>

<script>
import moment from 'moment'
import ArticlesAdd from '@/module-hmmm/components/articles-add'
import ArticlesPreview from '@/module-hmmm/components/articles-preview'
import { list, remove, changeState } from '@/api/hmmm/articles.js'
export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      valUrl: null,
      isShow: false,
      isDialogFormVisible: false,
      dialogFormVisible: false,
      formBase: {
        videoURL: '',
        articleBody: '',
        title: ''
      },
      form: {
        keyword: '',
        state: null
      },
      query: {
        page: 1,
        pagesiae: 10
      },
      total: 0,
      randerList: [],
      previewData: {}
    }
  },
  async created () {
    this.initData()
  },
  methods: {
    handleVal () {
      this.isShow = false
    },
    val (val) {
      this.isShow = true
      this.valUrl = val
    },
    //  获取数据渲染页面
    async initData () {
      const { data } = await list(this.query)
      console.log(data)
      this.total = data.counts
      this.randerList = data.items
      this.randerList.forEach(item => {
        item.state = item.state ? '已启用' : '已禁用'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 点击搜索发生什么
    async onSubmit () {
      const { data } = await list(this.form)
      this.randerList = data.items
      this.total = data.counts
      this.randerList.forEach(item => {
        item.state = item.state ? '已启用' : '已禁用'
        item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    // 点击清空触发事件
    resetForm () {
      this.form.state = ''
      this.form.keyword = ''
      this.initData()
    },
    preview (id) {
      this.randerList.forEach(item => {
        this.isDialogFormVisible = true
        if (item.id === id) {
          console.log(item)
          item.addDate = moment(item.addDate).format('YYYY-MM-DD HH:mm:ss')
          this.previewData = item
        }
      })
    },
    // 点击添加或者修改
    openDrawer (type, id) {
      this.dialogFormVisible = true
      console.log(id)
      if (type !== 'add') {
        this.randerList.forEach(item => {
          if (item.id === id) {
            this.formBase.id = id
            this.formBase.videoURL = item.videoURL
            this.formBase.articleBody = item.articleBody
            this.formBase.title = item.title
          }
        })
      } else {
        this.formBase.id = ''
        this.formBase.videoURL = ''
        this.formBase.articleBody = ''
        this.formBase.title = ''
      }
    },
    async isState (id, state) {
      const obj = { id, state }
      if (obj.state === '已启用') {
        obj.state = 0
      } else {
        obj.state = 1
      }
      await changeState(obj)
      this.initData()
    },
    async delDrawer (id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id })
          this.initData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async newDataes (data) {
      console.log(data)
      this.initData()
    },
    // 分页点击
    handleSizeChange (val) {
      this.query.pagesize = val
      this.query.page = 1
      this.initData()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.initData()
    }
  }
}
</script>

<style scoped lang="less">
// 表单
.container{
  position: relative;
}
.demo-form-inline {
  width: 1300px;
  padding: 0 20px;
  background-color: #fff;
  margin: 20px;
  .users_main_head {
    width: 100%;
    padding: 20px 0;
  }
}
/deep/.users_main_head_search {
  width: 200px !important;
  height: 32px;
}
// 新增按钮
.newElement {
  margin-left: 450px;
}
.uesrs_main_statistics {
  font-size: 13px;
  background-color: #f4f4f5;
  color: #909399;
  padding: 10px 20px;
}
a {
  margin-right: 8px;
  color: #409eff;
}
.el-pagination {
  margin-left: 610px;
  margin-top: 20px;
}
.operation {
  border: none;
  width: 30px;
  height: 35px;
  background-color: rgba(red, green, blue, 1);
  color: #c0c4cc;
}
.el-icon-film {
  color: #00f;
  font-size: 18px;
}
</style>
